<?php

/**
 * Project: CrazyPoems
 * Author: Alexandru
// Traslate By: Christian :P
 * Date: 21/10/2008
 * File: new-poem.php
 */

// Load default settings.
require('inc/config.php');
require('inc/functions.misc.php');
require('inc/functions.index.php');
require('inc/functions.poem.php');

// Check if user is logged in.
if (!isset($cookie->username) || strlen(trim($cookie->username)) == 0) { header("Location: ".BASE_URL."login.php?next=".get_full_url()); }

$smarty->assign('pageTitle', l('Agregar poesía'));
$smarty->assign('breadcrumbs', '<a href="'.BASE_URL.'">'.l('Inicio').'</a> &raquo; '. l('Agregar poesía'));

// Define variables.
$error_msg = "";
$title = isset($_POST['title']) && strlen($_POST['title']) > 0 ? stripslashes($_POST['title']) : '';
$lang = isset($_POST['lang']) && strlen($_POST['lang']) > 0 ? $_POST['lang'] : '';
$category = isset($_POST['category']) && strlen($_POST['category']) > 0 ? $_POST['category'] : '';
$poem = isset($_POST['poem']) && strlen($_POST['poem']) > 0 ? stripslashes($_POST['poem']) : '';
$response_to = isset($_REQUEST['response_to']) && strlen($_REQUEST['response_to']) > 0 ? $_REQUEST['response_to'] : '';

// Validate data.
if (isset($_POST['form_sent']) && $_POST['form_sent'] == "yes") {
	if (strlen($title) == 0) {
		$error_msg .= "<p> Introdu titlu. </p>";
	}
	if (strlen($category) == 0) {
		$error_msg .= "<p> Alege o categorie. </p>";
	}
	if (strlen($poem) == 0) {
		$error_msg .= "<p> Introdu poezia. </p>";
	}
	// Add.
	if (strlen($error_msg) == 0) {
        $smarty->assign('lastId', Hop_newPoem($title, $category, $poem, $response_to));
	}
} else {
	$_POST['author'] = $cookie->username;
        $_POST['lang'] = $cookie->lang;
}

// Smarty.
$smarty->assign('form_sent', $_POST['form_sent']);
$smarty->assign('error_msg', $error_msg);
$smarty->assign('title', $title);
$smarty->assign('category', $category);
$smarty->assign('poem', $poem);
$smarty->assign('responseTo', $response_to);
$smarty->assign('categories', getCategories($category));
$smarty->assign('authors', getAllMembers());

// Go!
$smarty->display('new-poem.tpl');

// End page.
require('inc/end.php');
?>
